
#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
const int INF = 555;
char g[INF][INF];
int dir[][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
bool vis[INF][INF];
int n, m;

struct Node {
    int x, y;
    int step;
    Node(int xx, int yy, int s):x(xx), y(yy), step(s){};
    Node(){}
};

int bfs(Node s) {
    queue<Node> q;
    q.push(s);
    vis[s.x][s.y] = true;
    while(!q.empty()) {
        Node f = q.front();
        q.pop();
        int step = f.step + 1;
        for(int i = 0; i < 4; i++) {
            int tx = f.x + dir[i][0];
            int ty = f.y + dir[i][1];

            while(g[tx][ty] != '#' && tx >= 0 && tx < n && ty >= 0 && ty < m) {
                if(g[tx][ty] == 'T') {
                    return step;
                }
                tx = tx + dir[i][0];
                ty = ty + dir[i][1];
            }
            tx = tx - dir[i][0];
            ty = ty - dir[i][1];
            if(!vis[tx][ty]) {
                vis[tx][ty] = true;

                q.push(Node(tx, ty, step));
            }
        }
    }
    return -1;
}
int main() {
   while(scanf("%d%d", &n, &m) != EOF) {
    Node s;int flag = 0;
    memset(vis, false, sizeof(vis));
    memset(g, '\0', sizeof(g));
    for(int i = 0; i < n; i++) {
        scanf("%s", g[i]);

        for(int j = 0; j < m; j++) {
            if(g[i][j] == 'S') {
                s.x = i;
                s.y = j;
                flag = 1;
                s.step = -1;
            }

        }

    }
    if(!flag) {
        printf("-1\n");
        continue;
    }
     int res = bfs(s);
     printf("%d\n", res);
   }
   return 0;
}
